/**
 * @author starsea
 * @date 2024-08-12 16:29
 */

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @author starsea
 * @date 2024-08-10 10:16
 */

public class SeleniumTest {
    public static void main(String[] args) throws InterruptedException, IOException {

        //Test01();
        //test02();
        //test03();
        Test019();


    }
    //上传文件
    private static void Test019() {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("file:///D:/Users/18212/selenium/AutoTestSelenium/src/main/java/page/test05.html");
        //上传文件
        webDriver.findElement(By.cssSelector("body > input[type=file]")).sendKeys("C:\\Users\\18212\\Downloads\\chromedriver-win64 (1).zip");


    }

    private static void Test018() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/Users/18212/selenium/AutoTestSelenium/src/main/java/page/test04.html");
        //点击弹窗
        webDriver.findElement(By.cssSelector("body > button")).click();
        sleep(1000);
        webDriver.switchTo().alert().sendKeys("陈圆");
        sleep(3000);
        webDriver.switchTo().alert().accept();
        WebElement element = webDriver.findElement(By.cssSelector("body > div"));
        if ("陈圆".equals(element.getText())) {
            System.out.println("测试通过");
        } else {

            System.out.println("测试不通过");
        }
        sleep(1000);
        webDriver.quit();

    }

    //下拉框
    private static void Test017() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("");
        //找到输入框元素
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //输入框输入“软件按测试”
        search_input.sendKeys("软件测试");
        //借助select
        Select select = new Select(search_input);
        //根据索引从0开始
        select.selectByIndex(1);
        //根据值
        select.selectByValue("111");
    }

    private static void Test016() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///D:/Users/18212/selenium/AutoTestSelenium/src/main/java/page/inner.html");
        //切换窗口
        webDriver.switchTo().frame("f1");
        sleep(1000);
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void Test015() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开页面
        webDriver.get("file:///D:/Users/18212/selenium/AutoTestSelenium/src/main/java/page/test01.html");
        List<WebElement> elements = webDriver.findElements(By.cssSelector("input"));
        for (WebElement element : elements) {
            if ("checkbox".equals(element.getAttribute("type"))) {
                element.click();
            }
        }

    }

    private static void Test014() throws InterruptedException, IOException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到输入框元素
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //输入框输入“软件按测试”
        search_input.sendKeys("软件测试");
        sleep(1000);
        //知道百度一下
        WebElement button = webDriver.findElement(By.cssSelector("#su"));
        sleep(1000);
        //点击
        button.click();
        //截图

        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        //保存
        FileUtils.copyFile(file, new File("D://jietu.png"));
        sleep(1000);
        webDriver.quit();

    }

    private static void Test013() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //点击新闻按钮
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        webDriver.manage().timeouts().implicitlyWait(5, SECONDS);
        //校验跳转页面
        if ("https://news.baidu.com/".equals(webDriver.getCurrentUrl())) {
            System.out.println("跳转成功");
        } else {
            System.out.println("跳转失败");
        }
        //找到当前句柄
        String cur = webDriver.getWindowHandle();
        //获取窗口句柄
        Set<String> all = webDriver.getWindowHandles();
        //找到最新的
        String tar = "";
        for (String tmp : all) {
            tar = tmp;
        }
        //切换窗口
        webDriver.switchTo().window(tar);
        //找到输入框输入经济新闻，并回车
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("经济新闻", Keys.ENTER);
        sleep(3000);
        //校验
        List<WebElement> elements = webDriver.findElements(By.xpath("//em[text()=\"经济\"]"));
        for (WebElement webElement : elements) {
            System.out.println(webElement.getText());
            if ("经济".equals(webElement.getText())) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }
        }
        webDriver.quit();

    }

    private static void Test012() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到输入框
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //输入鲜花并回车
        element.sendKeys("鲜花", Keys.ENTER);
        //鼠标操作
        Actions actions = new Actions(webDriver);
        sleep(3000);
        //鼠标移动到资讯
        //perform();显示效果
        actions.moveToElement(webDriver.findElement(By.xpath("//*[@id=\"s_tab\"]/div/a[2]"))).perform();
        actions.doubleClick().perform();
        webDriver.manage().timeouts().implicitlyWait(5, HOURS);
        webDriver.quit();


    }

    private static void Test011() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到输入框
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //输入鲜花
        element.sendKeys("鲜花");
        //sendKeys(Keys.CONTROL,'a') #全选（Ctrl+A）
        //
        //sendKeys(Keys.CONTROL,'c') #复制（Ctrl+C）
        //
        //sendKeys(Keys.CONTROL,'x') #剪切（Ctrl+X）
        //
        //sendKeys(Keys.CONTROL,'v') #粘贴（Ctrl+V）
        //全选
        element.sendKeys(Keys.CONTROL, "A");
        sleep(3000);
        System.out.println(element.getText());
        //剪贴
        element.sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        System.out.println(element.getText());
        //粘贴
        element.sendKeys(Keys.CONTROL, "V");
        sleep(3000);
        System.out.println(element.getText());
        //回车
        element.sendKeys(Keys.ENTER);
        sleep(3000);
        //校验
        List<WebElement> elements = webDriver.findElements(By.xpath("//font[text()=\"鲜花\"]"));
        for (WebElement webElement : elements) {
            System.out.println(webElement.getText());
            if ("鲜花".equals(webElement.getText())) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }
        }
        webDriver.quit();
    }

    private static void Test010() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到输入框
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //输入鲜花并回车
        //element.sendKeys("鲜花",Keys.ENTER);
        //输入鲜花
        element.sendKeys("鲜花");
        //回车
        element.sendKeys(Keys.ENTER);
        sleep(3000);
        //校验
        List<WebElement> elements = webDriver.findElements(By.xpath("//font[text()=\"鲜花\"]"));
        for (WebElement webElement : elements) {
            System.out.println(webElement.getText());
            if ("鲜花".equals(webElement.getText())) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }
        }
        webDriver.quit();


    }

    private static void Test09() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到输入框
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //输入鲜花
        element.sendKeys("鲜花");
        //点击百度一下
        webDriver.findElement(By.cssSelector("#su")).click();
        //等待三秒
        sleep(3000);
        //将滚动条滑倒最下面,s->滚动条的js
        ((JavascriptExecutor) webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(3000);
        webDriver.quit();
    }

    private static void Test08() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到搜索框
        WebElement webElement = webDriver.findElement(By.cssSelector("#kw"));
        //输入张三
        webElement.sendKeys("张三");
        //点击百度一下的按钮
        webDriver.findElement(By.cssSelector("#su")).submit();
        //浏览器回退到上一个
        webDriver.navigate().back();
        webDriver.navigate().to("https://www.baidu.com/");
        //浏览器前进
        webDriver.navigate().forward();
        String url = webDriver.getCurrentUrl();
        //检查url
        if ("https://www.baidu.com/".equals(url)) {
            System.out.println("浏览器前进成功");
        } else {
            System.out.println("前进失败");
            System.out.println(url);
        }
        sleep(3000);
        webDriver.quit();


    }

    private static void Test07() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //浏览器最大化
        webDriver.manage().window().maximize();
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(200, 200));
        sleep(3000);
        //定位设置元素
        WebElement element = webDriver.findElement(By.cssSelector("#s-usersetting-top"));
        //校验存不存在
        webDriver.quit();

    }

    private static void Test06() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com/");
        String title = webDriver.getTitle();
        System.out.println(title);
        //拿到url
        String url = webDriver.getCurrentUrl();
        System.out.println(url);
        if (url.equals("https://www.baidu.com/")) {
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
        webDriver.manage().timeouts().implicitlyWait(3, HOURS);
        webDriver.quit();


    }

    private static void Test05() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //找到新闻按钮
        WebElement button = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        //点击新闻按钮
        //button.click();
        //隐式等待
        webDriver.manage().timeouts().implicitlyWait(5, HOURS);
        //显示等待
        WebDriverWait webDriverWait = new WebDriverWait(webDriver, 3);
        WebElement result = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#s-top-left > a:nth-child(1)")));
        sleep(3000);
        //退出新闻
        webDriver.quit();

    }

    private static void Test04() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //找到新闻按钮
        WebElement button = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        //点击新闻按钮
        button.submit();
        sleep(3000);
        //退出新闻
        webDriver.quit();
    }

    private static void test03() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //知道对应的元素
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //输入框输入“软件按测试”
        search_input.sendKeys("软件测试");
        sleep(1000);
        //知道百度一下
        WebElement button = webDriver.findElement(By.cssSelector("#su"));
        button.submit();
        sleep(5000);
        //校验，搜索结果没有软件测试，则测试不通过
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (WebElement element : elements) {
            if (element.getText().equals("软件测试")) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }

        }
        //找到新的页面上的百度搜索输入框
        WebElement reaschNewInput = webDriver.findElement(By.cssSelector(".s_ipt"));
        //清除输入框中的内容
        reaschNewInput.clear();

        //再次输入“前端知识”
        reaschNewInput.sendKeys("前端");
        WebElement button2 = webDriver.findElement(By.cssSelector("#su"));
        button.submit();
        //知道百度一下按钮
        button2.submit();
        sleep(5000);
        System.out.println("开始校验新查询出来的词条");
        //校验页面上的内容是否正常显示
        List<WebElement> list = webDriver.findElements(By.xpath("//font[text()=\"前端\"]"));
        for (WebElement element : list) {
            System.out.println(element.getText());
        }
        webDriver.quit();

    }

    private static void test02() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //找到输入框元素
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //输入框输入“软件按测试”
        search_input.sendKeys("软件测试");
        sleep(1000);
        //知道百度一下
        WebElement button = webDriver.findElement(By.cssSelector("#su"));
        sleep(1000);
        //点击
        button.click();
        //button.submit();与click效果相同click相当于鼠标点击
        //等待页面渲染

        sleep(5000);
        //校验，搜索结果没有软件测试，则测试不通过
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (WebElement element : elements) {
            if (element.getText().equals("软件测试")) {
                System.out.println("测试通过");
            } else {
                System.out.println("测试不通过");
            }

        }

        webDriver.quit();
        //关闭页面

    }

    private static void Test01() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //定位元素
        // WebElement search_input = webDriver.findElement(By.cssSelector(".s_ipt"));
        ////标签定位
        //WebElement search_input2 = webDriver.findElement(By.tagName("input"));
        ////通过xpath定位
        //
        //WebElement search_input1 = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        ////关闭浏览器

        webDriver.quit();//关的更彻底cookies都删了
        //webDriver.close();//对应的信息都没有删除
    }

}
